package com.xiaoshuidi.cloud.module.bpm.mq.listener;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventData;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventListener;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventTypeEnum;
import com.xiaoshuidi.cloud.framework.systemevent.core.data.LeaseSystemEventMobileModify;
import com.xiaoshuidi.cloud.module.bpm.service.swo.SwoTicketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class MobileModifyListener implements LeaseSystemEventListener {
    @Resource
    private SwoTicketService swoTicketService;

    @Override
    public LeaseSystemEventTypeEnum getEventType() {
        return LeaseSystemEventTypeEnum.MOBILE_MODIFY;
    }

    @Override
    public void onEvent(LeaseSystemEventData event) {
        LeaseSystemEventMobileModify data = (LeaseSystemEventMobileModify) event;
        log.info("收到修改手机消息 {}#######################", JsonUtils.toJsonString(data));

        try {
            if (ObjectUtils.isEmpty(data.getUserId())) {
                log.info("新用户id为空，ignore");
            } else {
                //bpm_swo_ticket
                swoTicketService.updateMember(data.getUserId(), data.getOldUserId(),data.getOldMobile(),data.getMobile(),data.getUserName());
            }
        } catch (Exception e) {
            log.error("修改手机消息处理异常", e.getMessage());
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "修改手机消息处理异常!");
        }
    }
}
